#include "bhxjglwaterwave.h"

BHXJGLWaterWave::BHXJGLWaterWave():BHXJGLModel()
{

}
void BHXJGLWaterWave::Init(QOpenGLFunctions_4_3_Core *arg)
{
    this->gl=arg;

    BHXJGLShader* shader=new BHXJGLShader();
    shader->Init(arg,":/WaterWave/waterwave.vert",":/WaterWave/waterwave.frag",":/WaterWave/waterwave.geom");
    this->shaders.push_back(shader);

    unsigned int segnum=100;

    auto lps=BHXJGLModelHelper::getLinePoints(segnum);
    BHXJGLVertex v;

    BHXJGLMesh *m=new BHXJGLMesh();
    for(unsigned int i=0;i<lps->size();++i)
    {
        for(unsigned int j=0;j<lps->size();++j)
        {
            v.position.x=lps->at(j).x;
            v.position.y=1-i*(2.0f/segnum);
            v.position.z=0;

            //            v.position.x*=2;
            //            v.position.y*=2;
            //            v.position.z=0;

            v.texcoodr.s=lps->at(j).x/2+0.5f;
            v.texcoodr.t=v.position.y/2+0.5f;
            m->vertices.push_back(v);
        }
    }
    for(unsigned int i=0;i<segnum;++i)
    {
        BHXJGLModelHelper::addStripeToMesh(m,i*lps->size(),(i+1)*lps->size(),segnum);
    }
    m->Init(gl);
    this->meshes.push_back(m);
}
void BHXJGLWaterWave::Paint(const BHXJGLPaintParam &p)
{
    shaders[0]->use();
    glm::vec3 parallellightdir= glm::vec3( p.Model() *glm::vec4(0,0,-1,0));
    shaders[0]->setVec3("parallellight.direction",parallellightdir);
    shaders[0]->setVec3("parallellight.ambient",glm::vec3(0.1,0.1,0.1));
    shaders[0]->setVec3("parallellight.diffuse",glm::vec3(0.5,0.5,0.5));
    shaders[0]->setVec3("parallellight.specular",glm::vec3(0.5,0.5,0.5));
    shaders[0]->setVec3("viewPos",_viewPos);


    //    glm::vec4 p0=mat*glm::vec4(0,1.3f,0.1f,1);
    //    glm::vec4 p1=mat*glm::vec4(0,0.48f,0.1f,1);

    //    shaders[0]->setVec3("segmentlight.position0",glm::vec3(p0) );
    //    shaders[0]->setVec3("segmentlight.position1",glm::vec3(p1));
    //    shaders[0]->setVec3("segmentlight.ambient",glm::vec3(0.0f,0.1f,0.0f));
    //    shaders[0]->setVec3("segmentlight.diffuse",glm::vec3(0.0f,0.1f,0.0f));
    //    shaders[0]->setVec3("segmentlight.specular",glm::vec3(0.0f,1.0f,0.0f));


    shaders[0]->setMat4("projection",p.Projection());
    shaders[0]->setMat4("view",p.View());
    shaders[0]->setMat4("model",p.Model());
    shaders[0]->setVec3("material.ambient",glm::vec3(0.0,0.7f,1.0f));
    shaders[0]->setVec3("material.diffuse",glm::vec3(0.0,0.7,1.0));
    shaders[0]->setVec3("material.specular",glm::vec3(0.0,0.7,1.0));
    shaders[0]->setVec3("material.emission",glm::vec3(0.01,0.05,0.01));
    shaders[0]->setFloat("material.shininess",1.f);


    //    shaders[0]->setFloat("water.L",1.f);
    //    shaders[0]->setFloat("water.A",0.3f);
    //    shaders[0]->setFloat("water.S",1.f);
    //    shaders[0]->setFloat("water.t",_t);
    //    shaders[0]->setVec2("water.D",glm::vec2(0,1));

//    shaders[0]->setFloat("water0.L",1);//0.5f);
//    shaders[0]->setFloat("water0.A",0.2f);
//    shaders[0]->setFloat("water0.S",0.3f);
//    shaders[0]->setFloat("water0.t",_t);
//    shaders[0]->setVec2("water0.D",glm::vec2(0,1));

    //    shaders[0]->setFloat("water1.L",1.5f);
    //    shaders[0]->setFloat("water1.A",0.1f);
    //    shaders[0]->setFloat("water1.S",0.1f);
    //    shaders[0]->setFloat("water1.t",_t);
    //    shaders[0]->setVec2("water1.D",glm::vec2(-1,-1));


    shaders[0]->setFloat("water[0].L",1);//0.5f);
    shaders[0]->setFloat("water[0].A",0.01f);
    shaders[0]->setFloat("water[0].S",0.5f);
    shaders[0]->setFloat("water[0].t",_t);
    shaders[0]->setVec2("water[0].D",glm::vec2(0,1));

    shaders[0]->setFloat("water[1].L",0.3f);//0.5f);
    shaders[0]->setFloat("water[1].A",0.02f);
    shaders[0]->setFloat("water[1].S",0.3f);
    shaders[0]->setFloat("water[1].t",_t);
    shaders[0]->setVec2("water[1].D",glm::vec2(1,0));


    shaders[0]->setFloat("water[2].L",0.5);//0.5f);
    shaders[0]->setFloat("water[2].A",0.03f);
    shaders[0]->setFloat("water[2].S",0.3f);
    shaders[0]->setFloat("water[2].t",_t);
    shaders[0]->setVec2("water[2].D",glm::vec2(1,1));

    shaders[0]->setFloat("water[3].L",0.1f);//0.5f);
    shaders[0]->setFloat("water[3].A",0.003f);
    shaders[0]->setFloat("water[3].S",0.6f);
    shaders[0]->setFloat("water[3].t",_t);
    shaders[0]->setVec2("water[3].D",glm::vec2(-1,-5));

    shaders[0]->setFloat("water[4].L",1.5);//0.5f);
    shaders[0]->setFloat("water[4].A",0.03f);
    shaders[0]->setFloat("water[4].S",0.7f);
    shaders[0]->setFloat("water[4].t",_t);
    shaders[0]->setVec2("water[4].D",glm::vec2(1,-1));


    meshes[0]->Paint(shaders[0]);

}
